#include "StdAfx.h"
#include "WebPage.h"

CWebPage::CWebPage(void)
{
	//m_axWindow;
	m_pWebBrowser2 = NULL;
	m_dispWebBrowser2 = NULL;
	m_id = 0;
	m_hWndWeb = NULL;

	m_pHtmlWin2 = NULL;
	m_pHtmlDoc2 = NULL;
	m_pHtmlDoc3 = NULL;

	m_data = 0;
	m_pVoid = NULL;
}

CWebPage::~CWebPage(void)
{
	CloseWebBrowser();
}

bool CWebPage::CreateWebBrowser(HWND hWndParent, int id, RECT *pRectInClient)
{
	HRESULT hr = E_FAIL;
	RECT rctClient;
	if ( pRectInClient != NULL )
		rctClient = *pRectInClient;
	else
	{
		::GetClientRect(hWndParent, &rctClient);
	}

	LPOLESTR clsid = NULL;
	StringFromCLSID(CLSID_WebBrowser,  &clsid);
	m_hWndWeb = m_axWindow.Create(hWndParent, rctClient, clsid, WS_CHILD|WS_VISIBLE);	
	::CoTaskMemFree(clsid);
	if ( m_hWndWeb == NULL )
		return false;
	
	hr = m_axWindow.QueryControl(IID_IDispatch, (void**)&m_dispWebBrowser2);
	hr = m_dispWebBrowser2->QueryInterface(IID_IWebBrowser2, (void**)&m_pWebBrowser2);
	return true;
}

bool CWebPage::CloseWebBrowser()
{
	if ( m_dispWebBrowser2 == NULL )
		return false;

	if ( m_pWebBrowser2 != NULL )
	{
		m_pWebBrowser2->Release();
		m_pWebBrowser2 = NULL;
	}	

	if ( m_dispWebBrowser2 != NULL )
	{
		m_dispWebBrowser2->Release();
		m_dispWebBrowser2 = NULL;
	}	

	if ( m_pHtmlWin2 != NULL )
	{
		m_pHtmlWin2->Release();
		m_pHtmlWin2 = NULL;
	}

	if ( m_pHtmlDoc2 != NULL )
	{
		m_pHtmlDoc2->Release();
		m_pHtmlDoc2 = NULL;
	}

	if ( m_pHtmlDoc3 != NULL )
	{
		m_pHtmlDoc3->Release();
		m_pHtmlDoc3 = NULL;
	}

	m_hWndWeb = NULL;
	m_axWindow.DestroyWindow();
	return true;
}

bool CWebPage::ShowWebPage(bool bShow)
{
	::ShowWindow(m_hWndWeb, bShow ? SW_SHOW : SW_HIDE);
	return true;
}

bool CWebPage::ResizeWebPage(CSize szNew, CPoint pLeftTop)
{	
	::SetWindowPos(m_hWndWeb, NULL, pLeftTop.x, pLeftTop.y, szNew.cx, szNew.cy, SWP_NOACTIVATE);
	return true;
}


IHTMLDocument2* CWebPage::GetHtmlDocument(void)
{
	if ( m_pHtmlDoc2 == NULL )
		return NULL;

	m_pHtmlDoc2->AddRef();
	return m_pHtmlDoc2;
}

IHTMLWindow2* CWebPage::GetHtmlWindow(void)
{
	if ( m_pHtmlWin2 == NULL )
		return NULL;

	m_pHtmlWin2->AddRef();
	return m_pHtmlWin2;
}

IDispatch* CWebPage::GetWebDisptch(void)
{
	if ( m_dispWebBrowser2 == NULL )
		return NULL;

	m_dispWebBrowser2->AddRef();
	return m_dispWebBrowser2;
}



bool CWebPage::SetTitle(BSTR pszTitle)
{
	if ( m_hWndWeb == NULL )
		return false;

	::SetWindowText(m_hWndWeb, pszTitle);
	return true;
}

bool CWebPage::Navigate(LPCTSTR pszUrl)
{
	if ( m_pWebBrowser2== NULL || pszUrl == NULL )		
		return false;

	CComVariant var(pszUrl);
	HRESULT hr = m_pWebBrowser2->Navigate2(&var, NULL, NULL, NULL, NULL);
	return SUCCEEDED(hr);
}

void CWebPage::OnDocumentComplete()
{
	if ( m_pWebBrowser2 == NULL )
		return;

	if ( m_pHtmlWin2 != NULL )
	{
		m_pHtmlWin2->Release();
		m_pHtmlWin2 = NULL;
	}

	if ( m_pHtmlDoc2 != NULL )
	{
		m_pHtmlDoc2->Release();
		m_pHtmlDoc2 = NULL;
	}

	if ( m_pHtmlDoc3 != NULL )
	{
		m_pHtmlDoc3->Release();
		m_pHtmlDoc3 = NULL;
	}

	HRESULT hr = E_FAIL;
	CComPtr<IDispatch> spDispDoc;
	hr = m_pWebBrowser2->get_Document(&spDispDoc);
	if ( SUCCEEDED(hr) )
		hr = spDispDoc->QueryInterface(IID_IHTMLDocument2, (void**)&m_pHtmlDoc2);

	if ( SUCCEEDED(hr) )
		hr = spDispDoc->QueryInterface(IID_IHTMLDocument3, (void**)&m_pHtmlDoc3);

	if ( SUCCEEDED(hr) )
		hr = m_pHtmlDoc2->get_parentWindow(&m_pHtmlWin2);
}

void CWebPage::OnHtmlWindowScroll()
{
}